package com.sss.support.poi.transform;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sss.support.poi.ExcelReader;
import com.sss.support.poi.ModelCreator;
import com.sss.support.poi.ReaderRole;

public abstract class ModelTransformer <T> {

	private static final Logger log = LoggerFactory.getLogger(ModelTransformer.class);
	
	private File excelFile;
	
	private ReaderRole role;
	
	private ModelCreator<T> creator;
	
	protected ExcelReader<T> reader = new ExcelReader<T>();
	
	
	public List<T> doTransform() throws Exception {
		synchronized(this){
			try {
				return this.reader.readTo(new FileInputStream(getExcelFile()),getParserRole(),getCreator());
			} catch (FileNotFoundException e) {			
				log.error(e.getMessage(),e);
				throw e;
			} catch (IOException e) {
				log.error(e.getMessage(),e);
				throw e;
			} catch (Exception e){
				log.error(e.getMessage(),e);
				throw e;
			}
		}		
	}
	
	
	public File getExcelFile() {
		return this.excelFile;
	}
		
	public ReaderRole getParserRole(){
		return this.role;
	}
	
	protected void setParserRole(ReaderRole role){
		this.role = role;
	}
	protected void setExcelFile(File file){
		this.excelFile = file;
	}

	protected void setCreator(ModelCreator<T> creator) {
		this.creator = creator;
	}


	public ModelCreator<T> getCreator() {
		return creator;
	}
}